import java.util.StringTokenizer;
import java.util.Vector;


class P167_39643 {

	public static void main(String[] args) {
		int[] a = new int[8];
		
		for (int k = 0; k < a.length; k++) {
			a[k] = -1;
		}

		Vector solutions = new Vector();
		
		BT(a, -1, solutions);
		
//		for (int k = 0; k < solutions.size(); k++) {
//			System.out.println(Arrays.toString((int[])solutions.elementAt(k)));
//		}
		
		int cases = Integer.parseInt(readLn());
		
		for (int k = 0; k < cases; k++) {
			int[][] table = new int[8][8];
			
			read(table);
		
			int max = Integer.MIN_VALUE;
			
			for (int i = 0; i < solutions.size(); i++) {
				int sum = 0;
				int[] array = (int[]) solutions.elementAt(i);
				
				for (int j = 0; j < 8; j++) {
					sum += table[j][array[j]];
				}
				
				if (max < sum) max = sum;
			}
		
			String result = Integer.toString(max);
			
			for (int i = result.length(); i < 5; i++) {
				result = " " + result;
			}
			
			System.out.println(result);
			
		}
		
		
		
		
	}
	
	private static void read(int[][] table) {
		
		for (int k = 0; k < 8; k++) {
			StringTokenizer st = new StringTokenizer(readLn());
			
			for (int j = 0; j < 8; j++) {
				table[k][j] = Integer.parseInt(st.nextToken());
			}
			
		}
		
	}

	private static void BT(int[] a, int index, Vector solutions) {
		index++;
//		System.out.println(index);
		if (index > 7) {
			int[] solution = new int[a.length];
			
			for (int k = 0; k < solution.length; k++) {
				solution[k] = a[k];
				
			}
			solutions.addElement(solution);
			
			return;
		} 
			
		for (int k = 0; k < 8; k++) {
//			System.out.println(k + " " + index);
			a[index] = k;
			if (isValid(a, index, k)) {
				BT(a, index, solutions);
			}
		}
	}
	
	private static boolean isValid(int[] a, int index, int y) {
		
//		if (1 == 1) return true;
		
//		System.out.print("P167_39643.isValid() " + Arrays.toString(a) + " index: "  + index + " y: " + y);
		
		for (int k = 0; k < index; k++) {
			if (a[k] == y) {
//				System.out.println(" false");
				return false;
			}
		}
		
		int y0 = y - 1;
		int x0 = index - 1;
		
		while (y0 > -1 && x0 > -1) {
			if (a[x0] == y0) {
//				System.out.println(" false");
				return false;
			}
			
			y0--;
			x0--;
			
		}
		
		y0 = y + 1;
		x0 = index - 1;
		
		while (y0 < 8  && x0 > -1) {
			if (a[x0] == y0) {
//				System.out.println(" false");
				return false;
			}
			
			y0++;
			x0--;
			
		}
		
//		System.out.println(" true");
		return true;
	}

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}
	
}
